﻿using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using WorldView;

namespace Modes
{
    /// <summary>
    /// Represents the mode where the actual game takes place.
    /// </summary>
    public class GameMode : Mode
    {
        private World world
        {
            get;
            set;
        }

        private View view
        {
            get;
            set;
        }

        public event GlobalGameClasses.Eventhandler.Pause pause;

        /// <summary>
        /// Passes the Update call to the world or switches to pausemode if the user pressed escape.
        /// </summary>
        public override void Update()
        {
            oldKeyboard = keyboard;
            keyboard = Keyboard.GetState();
            if (KeyPressed(Keys.Escape))
            {
                pause();
            }
            world.Update(oldKeyboard, keyboard);
        }

        public override void Draw()
        {
            view.Draw();
        }

        public GameMode(SpriteBatch spriteBatch, View view, World world)
        {
            this.view = view;
            this.world = world;
            this.spriteBatch = spriteBatch;
        }

        private bool KeyPressed(Keys key)
        {
            return oldKeyboard.IsKeyDown(key) && keyboard.IsKeyUp(key);
        }
    }
}
